package cn.jbolt.ec.order;

import cn.jbolt.base.JBoltBaseController;
import cn.jbolt.common.bean.OptionBean;
import cn.jbolt.common.model.BecRetailerOrder;
import cn.jbolt.common.model.BecSysconfig;
import cn.jbolt.ec.order.handler.*;
import cn.jbolt.ec.sysconfig.SysconfigService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;

import java.util.ArrayList;
import java.util.List;

public class EcOrderController extends JBoltBaseController {
    @Inject
    TypeA typea;
    @Inject
    TypeB typeb;
    @Inject
    TypeC typec;
    @Inject
    TypeE typee;
    @Inject
    TypeO typeo;
    @Inject
    TypeP typep;
    @Inject
    PayService payService;
    @Inject
    OrderService orderService;
    @Inject
    OrderMsgService orderMsgService;
    @Inject
    SysconfigService sysconfigService;


    /**
     * 综合查询orders
     * 包括带和不带模板渲染
     * 模版渲染的给管理端用
     */
    public void orders(boolean temp) {
        if (temp) {
            render("orders.html");
            return;

        }
        renderJson("{ok:0}");


    }


    public void create(String openSession, int type, int retailer_id, String address,
                       String consignee, String mobile, String commoditiess, int price, int charge_id,
                       int commodity_id, int option_id) {

        JSONObject jo1 = new JSONObject();
        if (null != getSessionAttr(openSession + "_member_id" ) ) {

            int  client_id = getSessionAttr(openSession + "_member_id");
            //int client_id=6767;
            JSONObject jo = new JSONObject();
            if (type == 1) {
                int id = typea.handle(client_id, retailer_id, address,
                        consignee, mobile, commoditiess);

                if (id > 0) {
                    jo.put("id", id);
                    jo.put("ok", 0);
                    renderJson(jo);
                    return;
                }

            }

            if (type == 2) {
                int id = typeb.handle(client_id, retailer_id, price);
                System.out.println("id:" + id);

                if (id > 0) {
                    jo.put("id", id);
                    jo.put("ok", 0);
                    renderJson(jo);
                    return;
                }

            }
            // 商家充值
            if (type == 3) {
                int id = typec.handle(client_id, retailer_id, charge_id, type);

                if (id > 0) {
                    jo.put("id", id);
                    jo.put("ok", 0);
                    renderJson(jo);
                    return;
                }

            }

            // 平台充值
            if (type == 5) {
                int id = typee.handle(client_id, price);

                if (id > 0) {
                    jo.put("id", id);
                    jo.put("ok", 0);
                    renderJson(jo);
                    return;
                }

            }

            System.out.println("type:" + type);
            BecSysconfig sysconfig = sysconfigService.findById(2);
            if(sysconfig==null || sysconfig.getValue().equals("off")){
                jo1.put("ok", 1002);
                jo1.put("msg", "未开放");
                renderJson(jo1);
                return;

            }



            if (type == 14) {
                JSONObject jsonObject = typeo.handle(client_id, retailer_id,
                        commodity_id, option_id, consignee, mobile);
                if (0 == jsonObject.getIntValue("ok")) {
                    BecRetailerOrder becRetailerOrder = (BecRetailerOrder) jsonObject.get("order");
                    orderMsgService.spike_order_notify(becRetailerOrder);
                    orderMsgService.notify_retailer(becRetailerOrder);

                }
                renderJson(jsonObject);

                return;

            }
            if (type == 15) {
                JSONObject jsonObject = typep.handle(client_id, retailer_id, commodity_id, option_id, consignee, mobile);
                if (0 == jsonObject.getInteger("ok")) {
                    BecRetailerOrder becRetailerOrder = (BecRetailerOrder) jsonObject.get("order");
                    orderMsgService.spike_order_notify(becRetailerOrder);
                    orderMsgService.notify_retailer(becRetailerOrder);

                }
                renderJson(jsonObject);
                return;
            }

            jo.put("ok", 8);
            renderJson(jo);

        } else {
            jo1.put("ok", 1001);
            jo1.put("msg", "无效类型");
            renderJson(jo1);
        }

    }


    /**
     * 通过order_id获取订单商品
     *
     * @param order_id
     */
    public void query_order_commodity(String order_id) {
        renderJson(orderService.query_order_commodity(order_id));
    }

    /**
     * 根据订单状态获取会员的订单信息
     *
     * @param client_id
     * @param status
     * @param pageNumber
     * @param pageSize
     */
    public void member_order_info(Integer client_id, Integer status, Integer pageNumber, Integer pageSize) {
        renderJson(orderService.member_order_info(client_id, status, pageNumber, pageSize));
    }

    /**
     * 查询会员订单状态
     *
     * @param statuses   [5,6,7]  [5] 合法
     * @param pageNumber
     * @param pageSize
     */

    public void get_by_status_member(String statuses, int pageNumber, int pageSize, String openSession, int type) {
        JSONObject jo = new JSONObject();
        if (null != getSessionAttr(openSession + "_member_id")) {
            if (pageNumber < 1) pageNumber = 1;
            if (statuses == null || statuses.length() < 3) statuses = "[5]";
            if (pageSize < 5) pageSize = 5;

            int[] ss = JSON.parseObject(statuses, int[].class);
            renderJson(orderService.get_by_status(ss, type, pageNumber, pageSize, getSessionAttr(openSession + "_member_id"), 0));
        } else {
            jo.put("ok", 1001);
            jo.put("msg", "无效Session");
            renderJson(jo);
        }

    }

    /**
     * 查询商家的订单状态
     *
     * @param statuses
     * @param pageNumber
     * @param pageSize
     * @param openSession
     * @param type
     */
    public void get_by_status_retailer(String statuses, int pageNumber, int pageSize, String openSession, int type) {
        JSONObject jo = new JSONObject();
        if (null != getSessionAttr(openSession + "_retailer_id")) {
            if (pageNumber < 1) pageNumber = 1;
            if (statuses == null || statuses.length() < 3) statuses = "[5]";
            if (pageSize < 5) pageSize = 5;

            int[] ss = JSON.parseObject(statuses, int[].class);
            renderJson(orderService.get_by_status(ss, type, pageNumber, pageSize, 0, getSessionAttr(openSession + "_retailer_id")));
        } else {
            jo.put("ok", 1001);
            jo.put("msg", "无效类型");
            renderJson(jo);
        }

    }


    /**
     * ***************************************************
     * 下面为管理端方法，主要为更新数据
     * @author heqingyu
     * *******************************************
     */


    /**
     * 后台管理 主页
     */
    public void index() {
        set("pageData", orderService.get_retailer_order_page(getPageSize(), getPageNumber(),
                getKeywords(), getInt("retailerId"), getInt("isType"), getLong("onTime")));
        keepPara();
        render("index.html");
    }


    /**
     * select isType下拉数据源
     */
    public void get_retailerOrder_Type() {
        List<OptionBean> options = new ArrayList<OptionBean>();
        options.add(new OptionBean("正常", 1));
        options.add(new OptionBean("简易", 2));
        options.add(new OptionBean("特价", 3));
        options.add(new OptionBean("全民秒杀", 13));
        renderJsonData(options);
    }


}
